package com.example.androidmainshixun.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.example.androidmainshixun.R;

public class MineActivity extends AppCompatActivity {
    public Context mContext;
    private Button mButton1;
    private Button mButton2;
    private Button mButton3;
    private Button mButton4;
    private Button mButton5;
    private Button mButton6;
    private TextView musername_now;
    private Fragment mContentFragment;
    private FragmentManager mFragmentManager;
    private MineActivityFragment mMineFragment;

    @SuppressLint("MissingInflatedId")
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine);
        mButton1=findViewById(R.id.button1);
        mButton2=findViewById(R.id.cart);
        mButton3=findViewById(R.id.button3);
        mButton4=findViewById(R.id.button4);
        mButton5=findViewById(R.id.button5);
        mButton6=findViewById(R.id.button6);

        musername_now=findViewById(R.id.username_now);

        initFragment();
        changeFragment(mMineFragment);
        mFragmentManager.beginTransaction()
                .add(R.id.fragment_content_mine, mMineFragment).commitAllowingStateLoss();
        mContentFragment = mMineFragment;


        /*立即登录*/
       mButton1.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent=new Intent(MineActivity.this, LoginActivity.class);
               startActivity(intent);
           }
       });
       /*购物车*/
       mButton2.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent=new Intent(MineActivity.this, CartActivity.class);
               startActivity(intent);
           }
       });
       /*收藏*/
       mButton3.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent=new Intent(MineActivity.this, MyCollect.class);
               startActivity(intent);
           }
       });
       /*足迹*/
       mButton4.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent=new Intent(MineActivity.this, FootPrint.class);
               startActivity(intent);
           }
       });
       /*我的订单*/
       mButton5.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent=new Intent(MineActivity.this, SoldGoods.class);
               startActivity(intent);
           }
       });
       /*设置*/
        mButton6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MineActivity.this, SetUp.class);
                startActivity(intent);
            }
        });
    }
    private void initFragment() {
        mFragmentManager = getSupportFragmentManager();
        mMineFragment = new MineActivityFragment();
    }
    /*系统返回*/
    @Override
    public void onBackPressed() {
        // 调用 finish() 方法结束当前 Activity
        finish();
    }
    public void changeFragment(Fragment to) {
        if (mContentFragment != null && mContentFragment != to) {
            if (!to.isAdded()) {
                mFragmentManager.beginTransaction().hide(mContentFragment)
                        .add(R.id.fragment_content_mine, to).commitAllowingStateLoss();
            } else {
                mFragmentManager.beginTransaction().hide(mContentFragment)
                        .show(to).commitAllowingStateLoss();
            }
            mContentFragment = to;
        }
    }
}
